package com.kupurui.asteacher.ui.mine.setting;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.frame.util.AppManger;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.bean.UserInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.ui.MainActivity;
import com.kupurui.asteacher.ui.login.LoginAty;
import com.kupurui.asteacher.utils.UserManger;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 修改登录密码
 * Created by Administrator on 2017/7/17.
 */

public class AlterPwdAty extends BaseAty {

    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.edit_old_password)
    EditText editOldPassword;
    @Bind(R.id.edit_new_password)
    EditText editNewPassword;
    @Bind(R.id.edit_repassword)
    EditText editRepassword;
    @Bind(R.id.fbtn_change_password_submit)
    FButton fbtnChangePasswordSubmit;

    private String old_password = "";
    private String new_password = "";
    private String re_passwrd = "";


    @Override
    public int getLayoutId() {
        return R.layout.alter_pwd_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar,"修改登录密码");
    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.fbtn_change_password_submit})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.fbtn_change_password_submit:
                alterPwd();
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                UserManger.setIsLogin(false);
                UserManger.setUserInfo(new UserInfo());
                AppManger.getInstance().killActivity(MainActivity.class);
                AppManger.getInstance().killActivity(SettingAty.class);
                startActivity(LoginAty.class,null);
                finish();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void alterPwd(){
        old_password = editOldPassword.getText().toString();
        new_password = editNewPassword.getText().toString();
        re_passwrd = editRepassword.getText().toString();
        if (TextUtils.isEmpty(old_password)){
            showToast("请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(new_password)){
            showToast("请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(re_passwrd)){
            showToast("请确认密码");
            return;
        }
        showLoadingDialog("");
        new Teacher().changePwd(UserManger.getId(),old_password,new_password,re_passwrd,this,0);
    }

}
